##### 1) need to "reconstruct the table_comp tables for this approach!!
##### 2) need to check the total number of possible cases when using only 3of4 subset and when collapsing all 3 cases together!!
##### 3) it also doesn't work for 4 islands cases!!! CHECK this one first!!

patterns=read.table("patterns for thr_001_18112010.txt", header=TRUE)
count_cols_thr1=apply(patterns[,(1:42)],2,sum)
count_rows_thr1=apply(patterns[,(1:42)],1,sum)
t1=count_cols_thr1
t1=cbin(t1,names(t1))
colnames(t1)=c("thr001","patterns")

patterns=NULL
patterns=read.table("patterns for thr_002_18112010.txt", header=TRUE)
count_cols_thr2=apply(patterns[,(1:42)],2,sum)
count_rows_thr2=apply(patterns[,(1:42)],1,sum)

t2=count_cols_thr2
t2=cbin(t2,names(t2))
colnames(t2)=c("thr002","patterns")
compar_t1t2=merge(t1,t2)
compar_t1t2=compar_t1t2[,-2]
topo=read.table("topolgy_info_or_not_01.txt",header=TRUE)
table_comp=merge(compar_t1t2,topo)

patterns=NULL
patterns=read.table("patterns for thr_005_19112010.txt", header=TRUE)

count_cols_thr5=apply(patterns[,(1:42)],2,sum)
count_rows_thr5=apply(patterns[,(1:42)],1,sum)


t5=count_cols_thr5
t5=cbind(t5,names(t5))
colnames(t5)=c("thr005","patterns")
compar_t1t2t5=merge(compar_t1t2,t5)
topo=read.table("topolgy_info_or_not_01.txt",header=TRUE)
topo=topo[,-5]
table_comp=merge(compar_t1t2t5,topo)

#########################################################################################################################################################################################################################################
####code as it worked on 19.11.2010 when table_comp had only thr=0.01 and 0.02#####
##############################################################################code with access to columns made with $ and tested to create mt005#######
##### gives row numbers of favourable cases for our hypothesis
### (m,t) cases



mt=NULL
mt_total=NULL
mt_tot=NULL
case4f=NULL
case3f=NULL
case4t=NULL
case3t=NULL
Nb_fav4_mt001=NULL
Nb_tot4_mt001=NULL
Nb_fav4_mt002=NULL
Nb_tot4_mt002=NULL
Nb_fav3_mt001=NULL
Nb_tot3_mt001=NULL
Nb_fav3_mt002=NULL
Nb_tot3_mt002=NULL
Nb_fav4_mt005=NULL
Nb_tot4_mt005=NULL
Nb_fav3_mt005=NULL
Nb_tot3_mt005=NULL


mt=c(grep("(m,t)",table_comp$patterns, fixed=TRUE),grep("(t,m)", table_comp[,1], fixed=TRUE))
mt_total=grep("t",grep("m",table_comp$patterns, value=TRUE), value=TRUE) 
for(e in 1:length(mt_total))
	{
	hlp=which(table_comp$patterns==mt_total[e])
	mt_tot=c(mt_tot,hlp)
	}

#####separates favourable patterns into 3 or 4 islands ones that can be taken as possible events
for (n in mt)
	{
	if(table_comp[n,]$Nb_island==4 & table_comp[n,]$isl4_pat=="y")
		{
		case4f=c(case4f,n)
		}	
	if(table_comp[n,]$Nb_island==3 & table_comp[n,]$isl3_pat_only=="y")
		{
		case3f=c(case3f,n)
		}
	}
	
#####separates possible patterns into 3 or 4 islands ones that can be taken as possible events
for (n in mt_tot)
	{
	if(table_comp[n,]$Nb_island==4 & table_comp[n,]$isl4_pat=="y")
		{
		case4t=c(case4t,n)
		}	
	if(table_comp[n,]$Nb_island==3 & table_comp[n,]$isl3_pat_only=="y")
		{
		case3t=c(case3t,n)
		}
	}

###for 4 islands
##thr=0.01
Nb_fav4_mt001=sum(as.numeric(as.matrix(table_comp[case4f,2])))
Nb_tot4_mt001=sum(as.numeric(as.matrix(table_comp[case4t,2]
)))
assump4_mt001=matrix(c(Nb_fav4_mt001,Nb_tot4_mt001,(Nb_fav4_mt001/Nb_tot4_mt001),0.20), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases","freq.obs","freq. exp"),"(m,t)"))
#write.table(assump4_mt001,"assump4_mt001.txt")

##thr=0.02
Nb_fav4_mt002=sum(as.numeric(as.matrix(table_comp[case4f,3])))
Nb_tot4_mt002=sum(as.numeric(as.matrix(table_comp[case4t,3])))
assump4_mt002=matrix(c(Nb_fav4_mt002,Nb_tot4_mt002,(Nb_fav4_mt002/Nb_tot4_mt002),0.20), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
#write.table(assump4_mt002,"assump4_mt002.txt")

##thr=0.05
Nb_fav4_mt005=sum(as.numeric(as.matrix(table_comp[case4f,4])))
Nb_tot4_mt005=sum(as.numeric(as.matrix(table_comp[case4t,4])))
assump4_mt005=matrix(c(Nb_fav4_mt005,Nb_tot4_mt005,(Nb_fav4_mt005/Nb_tot4_mt005),0.20), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases","freq.obs","freq. exp"),"(m,t)"))
write.table(assump4_mt005,"assump4_mt005.txt")



###for 3 islands
##thr=0.01
Nb_fav3_mt001=sum(as.numeric(as.matrix(table_comp[case3f,2])))
Nb_tot3_mt001=sum(as.numeric(as.matrix(table_comp[case3t,2])))
assump3_mt001=matrix(c(Nb_fav3_mt001,Nb_tot3_mt001,(Nb_fav3_mt001/Nb_tot3_mt001),0.33), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
#write.table(assump3_mt001,"assump3_mt001.txt")

##thr=0.02
Nb_fav3_mt002=sum(as.numeric(as.matrix(table_comp[case3f,3])))
Nb_tot3_mt002=sum(as.numeric(as.matrix(table_comp[case3t,3])))
assump3_mt002=matrix(c(Nb_fav3_mt002,Nb_tot3_mt002,(Nb_fav3_mt002/Nb_tot3_mt002),0.33), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
#write.table(assump3_mt002,"assump3_mt002.txt")

##thr=0.05
Nb_fav3_mt005=sum(as.numeric(as.matrix(table_comp[case3f,4])))
Nb_tot3_mt005=sum(as.numeric(as.matrix(table_comp[case3t,4])))
assump3_mt005=matrix(c(Nb_fav3_mt005,Nb_tot3_mt005,(Nb_fav3_mt005/Nb_tot3_mt005),0.33), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
write.table(assump3_mt005,"assump3_mt005.txt")


##### gives row numbers of favourable cases for our hypothesis
### (m,t) cases
mt=NULL
mt_total=NULL
mt_tot=NULL
case4f=NULL
case3f=NULL
case4t=NULL
case3t=NULL
Nb_fav4_mt001=NULL
Nb_tot4_mt001=NULL
Nb_fav4_mt002=NULL
Nb_tot4_mt002=NULL
Nb_fav3_mt001=NULL
Nb_tot3_mt001=NULL
Nb_fav3_mt002=NULL
Nb_tot3_mt002=NULL


mt=c(grep("(m,t)",table_comp[,1], fixed=TRUE),grep("(t,m)", table_comp[,1], fixed=TRUE))
mt_total=grep("t",grep("m",table_comp[,1], value=TRUE), value=TRUE) # all cases where t and m appear
mt_tot=NULL
for(e in 1:length(mt_total))
	{
		hlp=which(table_comp[,1]==mt_total[e])
		mt_tot=c(mt_tot,hlp)
	}
	
	
#####separates favourable patterns into 3 or 4 islands ones that can be taken as possible events
for (n in mt)
	{
	if(table_comp[n,4]==4 & table_comp[n,5]=="y")
		{
			case4f=c(case4f,n)
		}	
	if(table_comp[n,4]==3 & table_comp[n,6]=="y")
		{
			case3f=c(case3f,n)
		}
	}
	
#####separates possible patterns into 3 or 4 islands ones that can be taken as possible events
for (n in mt_tot)
	{
	if(table_comp[n,4]==4 & table_comp[n,5]=="y")
		{
			case4t=c(case4t,n)
		}	
	if(table_comp[n,4]==3 & table_comp[n,6]=="y")
		{
			case3t=c(case3t,n)
		}
	}

###for 4 islands
##thr=0.01
Nb_fav4_mt001=sum(as.numeric(as.matrix(table_comp[case4f,2])))
Nb_tot4_mt001=sum(as.numeric(as.matrix(table_comp[case4t,2]
)))
assump4_mt001=matrix(c(Nb_fav4_mt001,Nb_tot4_mt001,(Nb_fav4_mt001/Nb_tot4_mt001),0.20), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases","freq.obs","freq. exp"),"(m,t)"))
write.table(assump4_mt001,"assump4_mt001.txt")

##thr=0.02
Nb_fav4_mt002=sum(as.numeric(as.matrix(table_comp[case4f,3])))
Nb_tot4_mt002=sum(as.numeric(as.matrix(table_comp[case4t,3]
)))
assump4_mt002=matrix(c(Nb_fav4_mt002,Nb_tot4_mt002,(Nb_fav4_mt002/Nb_tot4_mt002),0.20), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
write.table(assump4_mt002,"assump4_mt002.txt")


###for 3 islands
##thr=0.01
Nb_fav3_mt001=sum(as.numeric(as.matrix(table_comp[case3f,2])))
Nb_tot3_mt001=sum(as.numeric(as.matrix(table_comp[case3t,2]
)))
assump3_mt001=matrix(c(Nb_fav3_mt001,Nb_tot3_mt001,(Nb_fav3_mt001/Nb_tot3_mt001),0.33), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
write.table(assump3_mt001,"assump3_mt001.txt")


##thr=0.02
Nb_fav3_mt002=sum(as.numeric(as.matrix(table_comp[case3f,3])))
Nb_tot3_mt002=sum(as.numeric(as.matrix(table_comp[case3t,3])))

assump3_mt002=matrix(c(Nb_fav3_mt002,Nb_tot3_mt002,(Nb_fav3_mt002/Nb_tot3_mt002),0.33), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),"(m,t)"))
write.table(assump3_mt002,"assump3_mt002.txt")

#################################################################################################################################### as working before the 30.11.2010 to calculate implementation of frequency events for all other possible combinations of pairs of islands (x,y) with x,y=m,r,h,t

one=NULL
two=NULL
p_tot=NULL
p_total=NULL
p_fav=NULL
pairs=NULL
case4f=NULL
case4t=NULL
case3f=NULL
case3t=NULL
Nb_fav4_p001=NULL
Nb_tot4_p001=NULL
Nb_fav4_p002=NULL
Nb_tot4_p002=NULL
Nb_fav3_p001=NULL
Nb_tot3_p001=NULL
Nb_fav3_p002=NULL
Nb_tot3_p002=NULL
Nb_fav4_p005=NULL
Nb_tot4_p005=NULL
Nb_fav3_p005=NULL
Nb_tot3_p005=NULL
thr_p=NULL


for (p in pairs)
	{
	one=substr(p,2,2)
	two=substr(p,4,4)
	inv_p=paste("(",two,",",one,")",sep="")
	p_fav=c(grep(p,table_comp$patterns, fixed=TRUE),grep(inv_p, table_comp[,1], fixed=TRUE))
	p_total=grep(one,grep(two,table_comp$patterns, value=TRUE), value=TRUE) 
	for(e in 1:length(p_total))
		{
		hlp=which(table_comp$patterns==p_total[e])
		p_tot=c(p_tot,hlp)
		}
	
	for (n in p_fav)
		{
		if(table_comp[n,]$Nb_island==4 & table_comp[n,]$isl4_pat=="y")
			{
			case4f=c(case4f,n)
			}	
		if(table_comp[n,]$Nb_island==3 & table_comp[n,]$isl3_pat_only=="y")
			{
			case3f=c(case3f,n)
			}
		}
	for (n in p_tot)
		{
		if(table_comp[n,]$Nb_island==4 & table_comp[n,]$isl4_pat=="y")
			{
			case4t=c(case4t,n)
			}	
		if(table_comp[n,]$Nb_island==3 & table_comp[n,]$isl3_pat_only=="y")
			{
			case3t=c(case3t,n)
			}
		}

	for (t in 1:length(thr_p))
		{
	###for 4 islands
		Nb_fav4_p=sum(as.numeric(as.matrix(table_comp[case4f,(t+1)])))
	print(Nb_fav4_p)
		Nb_tot4_p=sum(as.numeric(as.matrix(table_comp[case4t,(t+1)])))
	assump4_p=matrix(c(Nb_fav4_p,Nb_tot4_p,(Nb_fav4_p/Nb_tot4_p),0.20), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases","freq.obs","freq. exp"),p))
		write.table(assump4_p,paste("assump4_",one,two,t,"test.txt",sep=""))

	###for 3 islands
		Nb_fav3_p=sum(as.numeric(as.matrix(table_comp[case3f,(t+1)])))
		Nb_tot3_p=sum(as.numeric(as.matrix(table_comp[case3t,(t+1)])))
	assump3_p=matrix(c(Nb_fav3_p,Nb_tot3_p,(Nb_fav3_p/Nb_tot3_p),0.33), nrow=4, ncol=1, dimnames=list(c("favourable cases", "total cases", "freq.obs","freq. exp"),p))
		write.table(assump3_p,paste("assump3_",one,two,thr_p[t],"test.txt",sep=""))
		}
	}


